import axios from 'axios'
import notification from 'ant-design-vue/es/notification'
import { VueAxios } from '@/utils/axios'
import defaultSettings from '@/config/defaultSettings'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import router from '@/router'


// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: 'http://192.168.1.200:8080/phm',
//   baseURL: 'http://127.0.0.1:9595/phm',
  timeout: 30000 // 请求超时时间
})

// 异常拦截处理器
const errorHandler = (error) => {
  if (error && error.response) {
    switch (error.response.status) {

        case 400:
            notification.error({ message: '系统提示', description: '错误请求'})
            break;
        case 401:

            // localStorage.clear();
            notification.error({ message: '权限已过期', description: '错误请求'})
            // router.push({ path: '/user/login'})
            break;
        case 403:
            notification.error({ message: '系统提示', description: '拒绝访问'})
            break;
        case 404:
            notification.error({ message: '系统提示', description:'很抱歉，资源未找到!'})
            break;
        case 405:
            notification.error({ message: '系统提示', description: '请求方法未允许'})
            break;
        case 408:
            notification.error({ message: '系统提示', description: '请求超时'})
            break;
        case 500:
            //默认发送错误都返回500
            notification.error({ message: '系统提示', description: '服务器端出错'})
            break;
        case 501:
            notification.error({ message: '系统提示', description: '网络未实现'})
            break;
        case 502:
            notification.error({ message: '系统提示', description: '网络错误'})
            break;
        case 503:
            notification.error({ message: '系统提示', description: '服务不可用'})
            break;
        case 504:
            notification.error({ message: '系统提示', description: '网络超时'})
            break;
        case 505:
            notification.error({ message: '系统提示', description: '网络超时'})
            break;
        default:
            notification.error({ message: '未知错误:'+error.response.status, description: '网络超时'})

    }
  } else {
      notification.error({ message: '连接到服务器失败', description: '网络超时'})

  }
  return Promise.reject(error);

}

// http interceptor
request.interceptors.request.use(config => {
    // console.log(config)

  return config
}, errorHandler)

// response interceptor
request.interceptors.response.use((response) => {
  return response.data
}, errorHandler)

const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, request)
  }
}

export default request

export {
  installer as VueAxios,
  request as axios
}
